package gui;

// Ptolemy imports
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import ptolemy.actor.Actor;
import ptolemy.actor.CompositeActor;
import ptolemy.actor.IOPort;
import ptolemy.data.BooleanToken;
import ptolemy.data.expr.SingletonParameter;
import ptolemy.kernel.ComponentEntity;
import ptolemy.kernel.util.Attribute;
import ptolemy.kernel.util.IllegalActionException;
import ptolemy.kernel.util.NameDuplicationException;
import ptolemy.kernel.util.NamedObj;
import ptolemy.kernel.util.Settable;
import ptolemy.kernel.util.StringAttribute;

import ptolemy.kernel.attributes.URIAttribute;
import java.net.MalformedURLException;
import java.net.URL;
import ptolemy.actor.gui.*;
import java.util.Enumeration;
import java.lang.ref.WeakReference;

import util.*;

//////////////////////////////////////////////////////////////////////////
//// VerifierInvoker
/**

    Xupado do chic

*/
public class VerifierInvoker extends Attribute {

    public VerifierInvoker(NamedObj container, String name)
            throws IllegalActionException, NameDuplicationException {
        super(container, name);

        _attachText("_iconDescription", "<svg>\n" +
                "<rect x=\"-50\" y=\"-25\" "
                + "width=\"100\" height=\"50\" "
                + "style=\"fill:white\"/>\n"
                + "<image x=\"-50\" y=\"-25\" width=\"100\" height=\"121\" "
                + "xlink:href=\"ptij/verifier/fasi.gif\"/>\n"
                + "</svg>\n");

        new VerifierControllerFactory(this, "_controllerFactory");

        _mocDialog = new MoCDialog(null);
        LinkedList mocNames = new LinkedList();
        mocNames.add(new String("SDF"));
        mocNames.add(new String("SR"));
        _mocDialog.setMoCs(mocNames);
    }

    ///////////////////////////////////////////////////////////////////
    ////                         public methods                    ////

    public void analyseSpec() {

        final Attribute fasi = this;
        
        Thread thread = new Thread(new Runnable() {
            public void run() {
                try {
                    _workspace.getReadAccess();

                    CompositeActor top = (CompositeActor) fasi.getContainer();
                    top = (CompositeActor) top.toplevel();

                    URIAttribute at = (URIAttribute) top.getAttribute("_uri");
                    URL url = null;
                    try {
                        url = at.getURL();
                    }
                    catch(MalformedURLException ex) {
                        System.out.println(ex);
                    }

                    VerifierEffigy effigy = null;
                    LinkedList selectedMoCs = _mocDialog.getSelectedMoCs();
                    if(selectedMoCs.size() == 0) {
                        // O mocDialog e modal
                        _mocDialog.show();
                        selectedMoCs = _mocDialog.getSelectedMoCs();
                        if(selectedMoCs.size() == 0) {
                            return;
                        }
                    }
                    String s = Util.removeDot(new String(url.toString() +
                        top.getFullName()));
        
                    ModelDirectory directory = null;
                    Configuration configuration = null;
                    
                    Iterator ite = top.getChangeListeners().iterator();
                    while(ite.hasNext()) {
                        WeakReference wref = (WeakReference) ite.next();
                        Object obj = wref.get();
                        if(obj instanceof PtolemyEffigy) {
                            PtolemyEffigy pteffigy = (PtolemyEffigy) obj;
                            
                            directory = (ModelDirectory) pteffigy.getContainer();
                            configuration = (Configuration) directory.getContainer();
                        }
                    }
                    
                    EffigyFactory factory =
                        (EffigyFactory) configuration.getEntity("effigyFactory");
                    EffigyFactory verifierFactory =
                        (EffigyFactory) factory.getEntity("Verifier");
        
                    effigy = (VerifierEffigy) directory.getEffigy(s);
                    if(effigy != null) {
                        // Achou a effigy, mas te que ver se foi modificada
                        if(!effigy.isModified()) {
                            effigy.showTableaux();
                            return;
                        }
                    }
        
                    // Cria uma nova effigy para a analise desejada
                    effigy = null;
                    try {
                        effigy = (VerifierEffigy) verifierFactory.createEffigy(directory);
                    }
                    catch(Exception ex) {
                        System.out.println("Nao consegui criar a VerifierEffigy -> " + ex);
                    }
                    if(effigy == null) return;
        
                    effigy.analyzeSpec(top, url, selectedMoCs);
                    
                    effigy.setConfiguration(configuration);
                    configuration.createPrimaryTableau(effigy);
                }
                finally {
                    _workspace.doneReading();
                }
            }
        });

        thread.start();
    }

    ///////////////////////////////////////////////////////////////////
    ////                         public variables                  ////


    ///////////////////////////////////////////////////////////////////
    ////                         protected methods                 ////
    
    ///////////////////////////////////////////////////////////////////
    ////                         private methods                   ////

    ///////////////////////////////////////////////////////////////////
    ////                         private variables                 ////

    private MoCDialog _mocDialog;
}
